//
//  ICWholeIntroduceView.m
//  IdealerClub
//
//  Created by 王芳 on 15/12/27.
//  Copyright © 2015年 IC. All rights reserved.
//

#import "ICWholeIntroduceView.h"
#import "MyPageControl.h"
#import "EFAutoScorllItem.h"
#import "EFGoodsHeaderItem.h"
#import "CycleScrollView.h"
#import "EFAutoImageView.h"
#import "EFEventPromotionViewController.h"

@interface ICWholeIntroduceView ()<UIScrollViewDelegate>
@property(nonatomic, strong) CycleScrollView *autoScrollView;
@property (nonatomic, strong) NSArray *autoSrolls;
@property (assign, nonatomic) NSInteger currentPage;
@property(nonatomic, strong)NSMutableArray *imgViewArray;
@end

@implementation ICWholeIntroduceView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.autoScrollView];
        __weak __typeof(&*self)weakSelf = self;
        self.autoScrollView.TapActionBlock = ^(NSInteger index) {
            [weakSelf onTapForIndex:index];
        };
    }
    return self;
}

- (void)onTapForIndex:(NSInteger)index{
    if (self.autoSrolls.count > index) {
//        EFAutoScorllItem *item = self.autoSrolls[index];
//        if (self.tapIntroduce) {
//            self.tapIntroduce(item);
//        }
//        if([APP_CONTEXT.accountManager isLogIn]){
//           
//            ICFindInvestorDetailViewController *vc = [[ICFindInvestorDetailViewController alloc] initWithItem:investorItem];
//            [ICNavigator pushViewController:vc animated:YES];
//        }else{
//            if (self.tapIntroduce) {
//                self.tapIntroduce(investorItem);
//            }
//            [ICLoginViewController presentLoginViewController:self withCompleteBlock:^{
//                ICFindInvestorDetailViewController *detail = [[ICFindInvestorDetailViewController alloc] initWithItem:investorItem];
//                [ICNavigator pushViewController:detail animated:YES];
//            }];
        }
}


- (void)updateUI:(NSArray *)array{
    self.autoSrolls = array;
    self.imgViewArray=nil;
    self.autoScrollView.totalPagesCount = ^(){
        NSInteger count = array.count;
        return count > 0 ? count : 1;
    };
    __weak typeof(self) weakSelf = self;
    self.autoScrollView.fetchContentViewAtIndex = ^(NSInteger pageIndex) {
        if (!weakSelf.imgViewArray) {
            NSMutableArray *arr = [NSMutableArray array];
            if( array.count >0 &&  [[array objectAtIndex:0 ] isKindOfClass:[EFAutoScorllItem class]]){
                for (EFAutoScorllItem *item in array) {
                    EFAutoImageView *imageV = [[EFAutoImageView alloc] initWithFrame:CGRectMake(0, 0,weakSelf.width , weakSelf.height)];
                    imageV.id = item.id;
                    imageV.backgroundColor = RGBCOLOR(0xf3f3f3);
                    imageV.userInteractionEnabled = YES;
                    [imageV setImageWithURL:[NSURL URLWithString:item.img]];
                    [imageV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:weakSelf action:@selector(turnToEventPage:)]];
                    imageV.contentMode = UIViewContentModeScaleAspectFill;
                    imageV.clipsToBounds = YES;
                    [arr addObject:imageV];
                }
            }else if(array.count >0 && [[array objectAtIndex:0 ] isKindOfClass:[EFGoodsHeaderItem class]]){
                for (EFGoodsHeaderItem *item in array) {
                    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,weakSelf.width , weakSelf.height)];
                    imageV.backgroundColor = RGBCOLOR(0xf3f3f3);
                    [imageV setImageWithURL:[NSURL URLWithString:item.imgUrl]];
                    imageV.contentMode = UIViewContentModeScaleAspectFill;
                    imageV.clipsToBounds = YES;
                    [arr addObject:imageV];
                }
            }
            //
            if (array.count == 0) {
                UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,weakSelf.width , weakSelf.height)];
                UIImage *image = [UIImage imageWithColor:RGBCOLOR(0xf3f3f3)];
                imageV.image = image;
                imageV.contentMode = UIViewContentModeScaleAspectFill;
                imageV.clipsToBounds = YES;
                [arr addObject:imageV];
            }
            weakSelf.imgViewArray = arr;
        }
        if (weakSelf.imgViewArray.count > pageIndex) {
            return weakSelf.imgViewArray[pageIndex];
        }

        return weakSelf.imgViewArray[0];
    };
    
}

- (void)turnToEventPage:(UITapGestureRecognizer *)tap{
    EFAutoImageView *imageV =(EFAutoImageView *)tap.view;
    NSString *eventId = imageV.id;
    EFEventPromotionViewController *vc = [[EFEventPromotionViewController alloc] initWithEventId:[NSNumber numberWithInteger:eventId.integerValue]];
    [ICNavigator pushViewController:vc animated:YES];
}

- (CycleScrollView *)autoScrollView{
    if (!_autoScrollView) {
        _autoScrollView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [_autoScrollView updatePageControlNormalDotImage:IMG(@"focusimage_icon_home_default") highlightDotImage:IMG(@"focusimage_icon_home_selected")];
        _autoScrollView.pageControlHorizontalAlignment = PageControlHorizontalAlignmentCenter;
    }
    return _autoScrollView;
}

- (void)stop{
    [self.autoScrollView stop];
}

@end
